#include <stdio.h>
#include <stdint.h>

int main (int argc, char** argv) {
    const int log_n = 10;
    const int n = 1<<log_n;
    const int p = 8;


    for (int me=0; me<p; me++) {            //demonstrate for every processor
       
        printf("\nFor processor %d\n",me);
        for (int level=0; level<log_n; level++) {
            printf("Level %d:\n",level);
            int parts = 1<<level;           //how many partitions exist
            int p_per_part = p/parts;       //how many processors per partitions are used
            int p_in_part = me%p_per_part;  //in our partition, we are the n-th processor
            int part = me/p_per_part;       //our partiton
            if ((1<<level)<p) {
                //decide which side is going to be swapped
                char* side = p_in_part>=p_per_part/2?"left":"right";    
                //with what processor do we want to exchange
                int sp = part*p_per_part+(me+p_per_part/2)%p_per_part;
                //print the result as simulation of data exchange
                printf("\t processor %d wants to swap its %s with %d\n",me,side,sp);
            } else {
                printf("Using sequential solution from level %d\n",level);
                break;
            }
        }
        
    }
    
}
